<?php
class CartController extends AppController {

	public $name = 'cart';

	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    //$this->C_auth->allowedActions = array('index','cartdel');

	}
	
	public function cartdel($id=0) {	
        $user_id = $this->my->id;	
        if(!isset($_GET['goods'])){
	        $goods = array(intval($_GET['id']));
        }else{
	        $goods=$_GET['goods'];
	        if (!$goods){
				$this->flash("请选择删除选项", Router::url('cart/index/'));
	        }
        }
		!isset($_GET['goods_id']) && $_GET['goods_id']='';
        $goods_id = is_array($_GET['goods_id'])?$_GET['goods_id']:array($_GET['goods_id']);
        if ($user_id) {
			$query = array(
				'conditions' => array('cart.user_id' => $user_id,'cart.id'=>$goods), 
				'fields' => array('cart.*'), 
			);
			$result = POP::exe('[@@:UM cart][@@:QU find:all]',$query);		
	        foreach ($result as $key=>$value){
		        $car=$this->C_session->read('_user_temp.cart');
		        if (isset($car[$value['cart']['goods_id']])) {
			        unset($car[$value['cart']['goods_id']]);
			        $this->C_session->write('_user_temp.cart', $car);
		        }
	        }
	        if(!$result) {
				$this->flash("购物车里不存在此商品！", Router::url('cart/index/')); 
	        }
        }elseif ((!$user_id)&&(!empty($goods_id))){
	        foreach ($goods_id as $value){
		        $car=$this->C_session->read('_user_temp.cart');
		        unset($car[$value]);
		        $this->C_session->write('_user_temp.cart', $car);
	        }
        }
        $err_str=0;
        if ($user_id) {
	        foreach ($result as $key=>$value){
				$query = array('conditions'=>array('cart.user_id' => $user_id,'cart.id' => $value['cart']['id']));							
		        if(POP::exe('[@@:UM cart][@@:QU del]',$query)) {
		        }else{
			        $err_str+=$key;
		        }
	        }
        }
        if ($err_str) {
		    $this->flash("购物车商品删除失败", '-1');
        }else {
		    $this->flash("购物车商品删除成功", Router::url('cart/index/'));
        }	
	}

	public function index() {
/*         $k = $_POST['k'];
        $cat = $_POST['cat']; */
		if(!isset(Request::$query['page']))Request::$query['page']=1;
        $page = intval(Request::$query['page']);		
        $user_id = $this->my->id;
		$query = array(
			'conditions' => array('cart.user_id' => $user_id), 
			'fields' => array('cart.*'), 
		);
		$rs = POP::exe('[@@:UM cart][@@:QU find:all]',$query);			

        $arr = array();
        foreach ($rs as $k=>$v){
	        $arr[]=$v['cart']['goods_id'];
        }
        $goods_ids="";
		$car=$this->C_session->read('_user_temp.cart');
        if ($car) {	
	        foreach ($car as $key=>$value){
		        if ($user_id) {
			        if (!in_array($key,$arr)) {
				        $insert_array = array(
					        'user_id' => $user_id,
					        'goods_id' => $key,
					        'goods_number' => $car[$key]['num'],
					        'add_time' => date("Y-m-d H:i:s",time()),
				        );
                        $query['cart'] = $insert_array;
                        POP::exe('[@@:UM cart][@@:QU save]',$query);						
			        }
		        }
		        $goods_ids.="($key,";
	        }
	        $this->C_session->write('_user_temp.cart', $car);
        }		
		$goods_ids.=")";
        if ($user_id) {
		
			$conditions = array();
			$conditions['cart.user_id'] = $user_id;	
			$url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
			$tb = array('model' => 'cart',
						'joins' => array(
						array('type'=>'left','table'=>'goods','conditions'=>array('cart.goods_id' =>'goods.id')),
						array('type'=>'left','table'=>'shopinfo','conditions'=>array('goods.shop_id' =>'shopinfo.id')),					
						),
						'fields' => array('shopinfo.user_id','cart.goods_id','cart.id','cart.add_time','cart.goods_number','goods.shop_id','goods.goods_name','goods.goods_thumb','goods.goods_price','goods.favpv','shopinfo.shop_name','shopinfo.id','shopinfo.lock_flg','shopinfo.open_flg'),
						'conditions' => $conditions,
						'order' => 'shopinfo.id desc,cart.add_time desc ',
						'limit' => '10',
						'group' => '',
						'url' => preg_replace("/&page=([^&]+)/","",$url_this),
						'page' => $page,
						'plist' => 6				
					); 
			$result = $this->fpage($tb); 		
		
        }	

		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'goods','conditions'=>array('cart.goods_id' =>'goods.id')),
				array('type'=>'left','table'=>'shopinfo','conditions'=>array('goods.shop_id' =>'shopinfo.id')),					
				),
				'fields' => array('sum(cart.goods_number*goods.goods_price) as sumvalue'),
				'conditions' => array('cart.user_id' =>$user_id),
				'order' => ' shopinfo.id desc,cart.add_time desc '
				);	
	    $row = POP::exe('[@@:UM cart][@@:QU find:all]',$query);			
        $row['sumvalue'] = $row[0][0]['sumvalue'] ;
        $shoparray=array();
        if(!empty($result['data'])){
	        foreach ($result['data'] as $k=>$v){
		        foreach ($v as $ke=>$va){
			        if($ke=="shop_id"){
				        $shoparray[$k]=$va['shop_id'];
			        }
		        }
	        }
        }
        $shoparray=array_unique($shoparray);		
        $this->set('result',$result);	
        $this->set('shoparray',$shoparray);	
        $this->set('row',$row);			
        $this->set('user_id',$user_id);		
		
	}
	
	
}

?>